//
//  BVConfirmOrderViewController.m
//  BuyVegetable
//
//  Created by yushanchang on 16/11/8.
//  Copyright © 2016年 com.yinding. All rights reserved.
//  确认订单

#import "BVConfirmOrderViewController.h"
#import "BVShoppingCart.h"
#define CONFIRM_CELL_ONE @"ConfirmCellOne"
#define CONFIRM_CELL_TWO @"ConfirmCellTwo"

@interface BVConfirmOrderViewController ()
<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UILabel *totalPrice;
@property (weak, nonatomic) IBOutlet UIButton *confirm;
@property (nonatomic ,strong) NSArray *titleArr;

@end

@implementation BVConfirmOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:@"确认下单"];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self updateTotalPrice];
}

- (IBAction)comfirmClick:(id)sender {
}

#pragma mark -
#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    } else if (section == 1) {
        return 3;
    } else {
        return self.dataSource.count + 2;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 50;
    } else if (indexPath.section == 1) {
        return 40;
    } else {
        return 30;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *confirm_cell;
    UITableViewCell *confirmTwo_cell;
    confirm_cell = [tableView dequeueReusableCellWithIdentifier:CONFIRM_CELL_ONE];
    confirmTwo_cell = [tableView dequeueReusableCellWithIdentifier:CONFIRM_CELL_TWO];
    
    if (indexPath.section == 0) {
        if (!confirm_cell) {
            confirm_cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CONFIRM_CELL_ONE];
            confirm_cell.textLabel.font = SYSTEM_FONT(14);
            confirm_cell.detailTextLabel.font = SYSTEM_FONT(11);
            confirm_cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        
        if (indexPath.section == 0) {
            confirm_cell.textLabel.text = @"未设置配送配送地点";
            confirm_cell.detailTextLabel.text = @"请先设置您的配送地点，以便于我们准确配送";
            confirm_cell.imageView.image = IMAGE_NAMED(@"dizhitubiao");
        }
        return confirm_cell;

    } else {
        
        if (!confirmTwo_cell) {
            confirmTwo_cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CONFIRM_CELL_TWO];
            if (indexPath.section == 1) {
                if (indexPath.row != 2) {
                    confirmTwo_cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                }
                confirmTwo_cell.textLabel.font = SYSTEM_FONT(14);
                confirmTwo_cell.detailTextLabel.font = SYSTEM_FONT(14);
                UILabel *lineView = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, ScreenW-30, 0.5)];
                lineView.backgroundColor = [UIColor BVLineGrayColor];
                [confirmTwo_cell.contentView addSubview:lineView];

            } else {
                confirmTwo_cell.textLabel.font = SYSTEM_FONT(13);
                confirmTwo_cell.detailTextLabel.font = SYSTEM_FONT(13);
                confirmTwo_cell.detailTextLabel.textColor= [UIColor BVBaseOrangeColor];
            }
        }
        
        if (indexPath.section == 1) {
            confirmTwo_cell.textLabel.text = self.titleArr[indexPath.row][@"title"];
            confirmTwo_cell.detailTextLabel.text = self.titleArr[indexPath.row][@"subTitle"];
        }
        if (indexPath.section == 2) {
            if (indexPath.row == 0) {
                confirmTwo_cell.textLabel.text = @"商品金额";
                confirmTwo_cell.detailTextLabel.text = self.totalPrice.text;
            } else if (indexPath.row == 1) {
                confirmTwo_cell.textLabel.text = @"优惠券抵扣";
                confirmTwo_cell.detailTextLabel.text = @"-￥0.00";
            } else {
                BVShoppingCart *shoppingcart = self.dataSource[indexPath.row - 2];
                confirmTwo_cell.textLabel.text = [NSString stringWithFormat:@"%@ x%ld",shoppingcart.title,shoppingcart.goods_num];
                confirmTwo_cell.detailTextLabel.text = [self updateEachPrice:indexPath.row - 2];
            }
        }
        return confirmTwo_cell;
    }
}

#pragma mark -
#pragma mark - 计算每个价格
- (NSString *)updateEachPrice:(NSInteger)index {
    float total_price = 0;
    BVShoppingCart *shoppingcart = self.dataSource[index];
    total_price = shoppingcart.goods_num * [shoppingcart.all_price floatValue];
    return [NSString stringWithFormat:@"￥%.2f",total_price];
}

#pragma mark -
#pragma mark - 计算总价格
- (void)updateTotalPrice {
    float total_price = 0;
    int i = 0;
    for (BVShoppingCart *shoppingcart in self.dataSource) {
        total_price += shoppingcart.goods_num * [shoppingcart.all_price floatValue];
        i++;
    }
    self.totalPrice.text = [NSString stringWithFormat:@"￥%.2f",total_price];
}

#pragma mark -
#pragma mark - UITableViewDelegate,UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return section == 1 ? 10 : section == 2 ? 30 : 0.01;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 2) {
        UIView *superView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 30)];
        
        UILabel *sectionThreeLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, ScreenW - 10, 30)];
        sectionThreeLabel.text = @"订单结算";
        sectionThreeLabel.textColor = [UIColor BVBaseTextBlackColor];
        sectionThreeLabel.font = SYSTEM_FONT(14);
        sectionThreeLabel.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [superView addSubview:sectionThreeLabel];
        return superView;
    } else {
        return [[UIView alloc] init];
    }
}

- (NSArray *)titleArr {
    if (!_titleArr) {
        _titleArr = @[@{@"title":@"订单备注",@"subTitle":@"点击添加备注"},@{@"title":@"优惠券",@"subTitle":@"点选择优惠券"},@{@"title":@"运费",@"subTitle":@"免运费"}];
    }
    return _titleArr;
}

@end
